package com.chebaohui.iqs.service.impl.deal.pingan.sys;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.service.impl.deal.pingan.login.PAHttpUtil;
import com.chebaohui.iqs.service.impl.deal.util.StringUtil;

/**
 * 投保跟踪
 * 
 * @author xielonghai
 *
 */
public class InsuredTracking {
	private String licenseNo;// 查询条件-车牌号
	private String frameNo;// 查询条件-车架号
	private String beginTime;// 查询条件-录入开始时间
	private String entTime;// 查询条件-录入结束时间
	private String type;// 查询条件-单证号类型
	/*
	 * 0-->子询价单号 1-->投保单号 3-->询价单号
	 */
	private String No;// 查询条件--单证号

	private String Cookie;// 根据进入平安新核心系统cookie 得到的Cookie--后续访问使用
	private Map<String, String> headers;// 查询头部
	private JSONObject param;// 查询参数

	private final String enterUrl = "https://icorepnbs.pingan.com.cn/icore_pnbs/apply_track.tpl?_v=1.63.3.50197&_v=1.63.3.50197";// 进入查询页面
																																	// get-url

	/**
	 * 构造查询类
	 * 
	 * @param racking
	 */
	private InsuredTracking(Tracking racking) {
		this.Cookie = racking.cookie;
		this.licenseNo = racking.licenseNo;
		this.frameNo = racking.frameNo;
		this.beginTime = racking.beginTime;
		this.entTime = racking.entTime;
		this.type = racking.type;
		this.No = racking.No;

		setHeaders();
	}

	/**
	 * 平安-->投保跟踪---查询
	 * 
	 * @return 查询结果
	 * @throws Exception
	 */
	public JSONObject check() throws Exception {
		if (null == Cookie || Cookie.isEmpty())
			throw new Exception("请先登录跳转后获取cookie");

		enter();

		if (beforeCheck()) {
			String inquire = inquire();

			return JSON.parseObject(inquire);
		} else
			throw new Exception("查询前访问返回错误");
	}

	/**
	 * 进入查询页面
	 */
	private void enter() {
		PAHttpUtil.GET(headers, enterUrl);
	}

	/**
	 * 查询前访问
	 * 
	 * @return
	 */
	private boolean beforeCheck() {
		String url = "https://icorepnbs.pingan.com.cn/icore_pnbs/do/app/cache/switch/getAcssFuntionSwitch";// POST-URL
		headers.put("Accept", "*/*");
		// headers.put("Content-Length", "52");
		headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		headers.put("Origin", "https://icorepnbs.pingan.com.cn");
		headers.put("Referer", enterUrl);
		headers.remove("Upgrade-Insecure-Requests");
		headers.put("X-Requested-With", "XMLHttpRequest");

		Map<String, String> param = new HashMap<>();
		param.put("departmentCode", "21825");
		param.put("switchName", "VATElectronicInvoice");
		String response = PAHttpUtil.POST(headers, url, param);
		JSONObject res = JSON.parseObject(response);
		return res.getBooleanValue("result");
	}

	/**
	 * 开始查询
	 * 
	 * @return
	 * @throws Exception
	 */
	private String inquire() throws Exception {
		String url = "https://icorepnbs.pingan.com.cn/icore_pnbs/do/app/workbench/qtWaitTaskInfo";
		headers.put("Accept", "application/json, text/plain, */*");
		headers.put("Content-Type", "application/json;charset=UTF-8");
		// headers.put("Content-Length", "351");
		headers.remove("X-Requested-With");
		JSONObject param = inquireCheck();
		return PAHttpUtil.POST_Json(headers, url, param.toJSONString());
	}

	/**
	 * 查询条件参数设置
	 * 
	 * @return
	 * @throws Exception
	 */
	private JSONObject inquireCheck() throws Exception {
		this.param = new JSONObject();
		param.put("applicantType", "");
		param.put("coinsuranceMark", "");
		if (!(StringUtil.isNulls(type) || StringUtil.isNulls(No))) {
			param.put("voucherNo", No);
			param.put("voucherType", type);
		} else if (!StringUtil.isNulls(frameNo) || !StringUtil.isNulls(licenseNo)) {
			param.put("applicantName", "");
			param.put("page", 1);
			param.put("personnelAttribute", "2");
			param.put("queryDateBegin", "");
			param.put("queryDateEnd", "");
			param.put("subTaskState", "");
			param.put("taskState", "");

			param.put("vehicleFrameNo", frameNo);
			param.put("vehicleLicenceCode", licenseNo);
		} else if (!(StringUtil.isNulls(beginTime) || StringUtil.isNulls(entTime))) {
			param.put("applicantName", "");
			param.put("isProduct", "isProduct");
			param.put("page", 1);
			param.put("personnelAttribute", "2");
			param.put("queryDateBegin", beginTime);
			param.put("queryDateEnd", entTime);
			param.put("subTaskState", "");
			param.put("taskState", "");
			param.put("vehicleFrameNo", "");
			param.put("vehicleLicenceCode", "");
		} else {
			throw new Exception("查询条件错误:车牌/车架号;单证号、单证号类型;录入开始、结束时间，三者条件之一");
		}

		param.put("applySeletemodel", "0");
		param.put("departmentCode", "21825");
		param.put("inputById", "CQHRBXDL-00002");
		param.put("lowDepartment", true);
		return param;
	}

	/**
	 * 设置公共头部
	 */
	private void setHeaders() {
		this.headers = new HashMap<>();
		headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		headers.put("Accept-Encoding", "gzip, deflate, br");
		headers.put("Accept-Language", "zh-CN,zh;q=0.8");
		headers.put("Connection", "keep-alive");
		headers.put("Cookie", Cookie);
		headers.put("Host", "icorepnbs.pingan.com.cn");
		headers.put("Referer", "https://icorepnbs.pingan.com.cn/icore_pnbs/do/usermanage/systemTransfer");
		headers.put("Upgrade-Insecure-Requests", "1");
		headers.put("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
	}

	/**
	 * Builder模式
	 * 
	 * @author xielonghai
	 *
	 */
	public static class Tracking {
		private String cookie;
		private String licenseNo;
		private String frameNo;
		private String beginTime;
		private String entTime;
		private String type;
		private String No;

		/**
		 * Builder模式构造器
		 * 
		 * @param cookie
		 */
		public Tracking(String cookie) {
			this.cookie = cookie;
			this.licenseNo = "";
			this.frameNo = "";
			this.beginTime = "";
			this.entTime = "";
			this.type = "";
			this.No = "";
		}

		/**
		 * 查询条件
		 * 
		 * @param licenseNo
		 *            车牌号
		 * @return
		 */
		public Tracking licenseNo(String licenseNo) {
			this.licenseNo = licenseNo;
			return this;
		}

		/**
		 * 查询条件
		 * 
		 * @param frameNo
		 *            车架号
		 * @return
		 */
		public Tracking frameNo(String frameNo) {
			this.frameNo = frameNo;
			return this;
		}

		/**
		 * 查询条件
		 * 
		 * @param beginTime
		 *            录入开始时间
		 * @param entTime
		 *            录入结束时间
		 * @return
		 */
		public Tracking enterTime(String beginTime, String entTime) {
			this.beginTime = beginTime;
			this.entTime = entTime;
			return this;
		}

		/**
		 * 查询条件
		 * 
		 * @param type
		 *            单证号类型
		 * @param No
		 *            单证号
		 * @return
		 */
		public Tracking numberNo(String type, String No) {
			this.type = type;
			this.No = No;
			return this;
		}

		/**
		 * 创建查询类
		 * 
		 * @return
		 */
		public InsuredTracking build() {
			return new InsuredTracking(this);
		}
	}
}
